if (typeof(parent._tocMenu)!="undefined") {
  parent._tocMenu.changeToSmall();
}
var _currentOrgId = null;
var _currentDeptId = null;
var _currentPosId = null;
var _currentRoleId = null;
var _activeNode = null;
var _orgInfoCache = []; //记录读取过的ORG对象
var _noPos = false;
//树的左键
function ATree_clickNode($node, $nameDom, $fireEvent) {
    _noPos = false;
    $nameDom.style.color = "red";
    _activeNode = $node;
    _currentDeptId = null;
    _currentPosId = null;
    _currentRoleId = null;
    _currentOrgId = null;
    if ($node.url=="O") { //组织单位
      _currentOrgId = _activeNode.key;
    } else if ($node.url=="D") { //部门
      _currentDeptId = _activeNode.key;
      _currentOrgId = ATree.getParent(_currentDeptId).key;
    } else if ($node.url=="P") { //岗位
      _currentPosId = _activeNode.key;
      _currentDeptId = ATree.getParent(_currentPosId).key;
      _currentOrgId = ATree.getParent(_currentDeptId).key
    } else if ($node.url=="L") { //无岗位员工
      _noPos = true;
    } else if ($node.url=="Role") { //角色
      if ($node.key.indexOf("@@")==-1) { //不是真正角色的节点
        return;
      }else {
        var k = _activeNode.key;
        k = k.substr(k.indexOf("@@")+2);
        _currentRoleId = k;
      }
    } else {
    }
    //筛选对应的人员列表信息
    reloadEmpLister();
}

function ATree_clickNodeChange($node, $nameDom) {
   $nameDom.style.color = "black";
}


//重新读取用户列表
function reloadEmpLister() {
    var lister = __get("EMPLister");
    var url = lister.getAttribute("tt.data.url");
    url = __setUrlParam(url, "orgId", __trim(_currentOrgId));
    url = __setUrlParam(url, "deptId", __trim(_currentDeptId));
    url = __setUrlParam(url, "posId", __trim(_currentPosId));
    url = __setUrlParam(url, "nopos", _noPos);
    url = __setUrlParam(url, "roleId", __trim(_currentRoleId));
    url = __setUrlParam(url, "search", "false");
    //加载人员信息表
    var selid={};
    selid["orgId"] = __trim(_currentOrgId);
    selid["deptId"]= __trim(_currentDeptId);
    selid["posId"] = __trim(_currentPosId);
    selid["nopos"] = _noPos;
    selid["roleId"]= __trim(_currentRoleId);
    selid["search"]= "false";
	doSelper(selid);
    //lister.setAttribute("tt.data.url", url); //原先人员信息加载
    //lister.reload();
    var sb = [];
    if (_noPos==true) {
      sb.push("无岗位用户");
    }else {
        if (_currentOrgId!=null) {
          sb.push(ATree.getNodeModel(_currentOrgId).name);
          if (_currentDeptId!=null) {
             sb.push("->");
             sb.push(ATree.getNodeModel(_currentDeptId).name);
             if (_currentPosId!=null) {
               sb.push("->");
               sb.push(ATree.getNodeModel(_currentPosId).name);
             }
          }
        }else {
           if (_currentRoleId!=null) {
             sb.push(ATree.getNodeModel("Role@@" + _currentRoleId).name);
           }
        }

    }
    __get("domEmpFilterDesc").innerHTML = sb.join("");
}

/** 树加载完毕 */
function ATree_loaded() {
  if (_activeNode==null) {
    ATree.clickByIndex(1);
  } else {
    var d = ATree.getDom(_activeNode.key);
    if (d==null) { //节点被删除了
      ATree.clickByIndex(1);
    } else {
      d = ATree.getName(_activeNode.key);
      if (d.style.color!="red") { //被重载了
        ATree.clickNode(_activeNode.key);
      }
    }
  }
}

    /**
     * TreeMouseMenu的回调方法
     */
    function __invokeAction($type, $fix) {
      if ($type.indexOf(".edit")!=-1) {
         __invokeEditAction($type, $fix);
         return;
      }
      if ($type=="root.org.add") { //增加根单位
        __xopen(__get("PanelRootOrgAdd"), "width=250;height=120;", $fix);
      }else if ($type=="org.add") { //增加子单位
        var p = __get("PanelOrgAdd");
        p.all.parentid.value = _activeNode.key;
        p.all.ParentName.innerHTML = _activeNode.name;
        __xopen(p, "width=250;height=120;", $fix);
      }else if ($type=="dept.add") { //增加部门
        var p = __get("PanelDeptAdd");
        p.all.parentid.value = _activeNode.key;
        p.all.ParentName.innerHTML = _activeNode.name;
        __xopen(p, "width=250;height=120;", $fix);
      }else if ($type=="pos.add") { //增加岗位
        var p = __get("PanelPosAdd");
        p.all.parentid.value = _activeNode.key;
        p.all.ParentNameOrg.innerHTML = ATree.getParent(_activeNode.key).name;
        p.all.ParentName.innerHTML = _activeNode.name;
        __xopen(p, "width=250;height=120;", $fix);
      }else if ($type=="pos.del") { //删除岗位
        if (!confirm("你确认要删除这个岗位[" + _activeNode.name + "]吗？")) return;
          var pid = deleteTree(_activeNode.key,'posid');
          __alert("删除岗位成功！");
          ATree.reload(pid);
          
      }else if ($type=="dept.del") { //删除部门
        if (!confirm("你确认要删除这个部门[" + _activeNode.name + "]吗？")) return;
          var pid = deleteTree(_activeNode.key,'deptid');
          __alert("删除部门成功！");
          ATree.reload(pid);
          
      }else if ($type=="org.del") {
        if (!confirm("你确认要删除这个单位[" + _activeNode.name + "]吗？")) return;
      	  var pid = deleteTree(_activeNode.key,'orgid');
          __alert("删除单位成功！");
          ATree.reload(pid);
          
      }else if ($type=="user.add") {
        doAddEmployee();
      }else if ($type=="role.add") {
        var roleUnitId = _activeNode.key;
        roleUnitId = roleUnitId.substr(roleUnitId.indexOf("@")+1);
        __open("role_add.jsp?roleUnitId=" + roleUnitId, "width=500;height=150;center=true;fresh=true");
      }else if ($type=="role.del") {
        if (!confirm("你确认要删除角色[" + _activeNode.name + "]吗？")) {
          return;
        }
        var roleUnitId = parseRoleKey(_activeNode.key);
        var parentKey = ATree.getParent(_activeNode.key).key;
        _Xuice.url("action/role_del_action.jsp?uuid=" + roleUnitId, finishR, false);
        function finishR($doc) {
          __alert("删除角色成功！");
          if (parentKey=="Role@unsortrole") {
            ATree.reload("Role@Tree");
          }else {
            ATree.reload(parentKey);
          }
        }
      }else if ($type=="role.perm") {
        var roleUnitId = parseRoleKey(_activeNode.key);
        __open("role_perm.jsp?roleUuid=" + roleUnitId, "relate=0.95;center=true;fresh=true");
      }else if ($type=="role.batch") { //批量关联角色至用户
        var roleUnitId = parseRoleKey(_activeNode.key);
        __open("role_batch.jsp?roleUuid=" + roleUnitId, "width=550;height=350;center=true;fresh=true");
      }
    }

    //角色关联权限成功
    function onRoleRermFinished() {
    }

    //角色批量关联至人成功
    function onRoleBatchFinish() {
      reloadEmpLister();
    }

    //解析角色的关键字
    function parseRoleKey($k) {
      return $k.substr($k.indexOf("@@")+2);
    }

    //增加角色完毕
    function onRoleAdded($m) {
      if (__isEmpty($m)) {
          ATree.reload("Role@Tree");
      }else {
        ATree.reload("Role@" + $m);
      }
    }

    //修改角色完毕
    function onRoleEdited($m) {
      ATree.reload("Role@Tree");
    }

    /**
     * 关闭窗口
     */
    function doCloseWin() {
      __close();
    }

    /**
     * 保存增加新的根单位
     */
    function doRootOrgAddSave() {
      var table = "Hzsysorganization";
      var names = "parentid,depname,deplink,depnumber,deporder,depcontent,deptype";
      var d = __getReal(event.srcElement, "id", "XuiceForm");
      var bo = saveXuiceForm( d ,names,table);
      if(bo=="null"){
      	return;
      }
      if(bo){
        __alert("增加根单位成功！");
      }else{
      	__alert("增加根单位失败！");
      }
      	ATree.reload();//刷新当前父节点
        doCloseWin();
    }
    /**
     * 增加子单位
     */
    function doOrgAddSave() {
	  var table = "Hzsysorganization";
      var names = "parentid,depname,deplink,depnumber,deporder,depcontent,deptype";
      var d = __getReal(event.srcElement, "id", "XuiceForm");
      var pid = d.all.parentid.value;
      var bo = saveXuiceForm( d ,names,table);
      if(bo=="null"){
      	return;
      }
      if(bo){
        __alert("增加子单位成功！");
        ATree.reload(pid);//刷新当前父节点
      }else{
      	__alert("增加子单位失败！");
      }
        doCloseWin();
    }
    /**
     * 增加部门
     */
    function doDeptAddSave() {
      var table = "Hzsysorganization";
      var names = "parentid,depname,deplink,depnumber,deporder,depcontent,deptype";
      var d = __getReal(event.srcElement, "id", "XuiceForm");
      var pid = d.all.parentid.value;
      var bo = saveXuiceForm( d ,names,table);
      if(bo=="null"){
      	return;
      }
      if(bo){
        __alert("增加部门成功！");
        ATree.reload(pid);//刷新当前父节点
      }else{
      	__alert("增加部门失败！");
      }
        doCloseWin();
    }
    /**
     * 增加岗位
     */
    function doPosAddSave() {
      var table = "Hzsysorganization";
      var names = "parentid,depname,depnumber,deporder,depcontent,deptype";
      var d = __getReal(event.srcElement, "id", "XuiceForm");
      var pid = d.all.parentid.value;
      var bo = saveXuiceForm( d ,names,table);
      if(bo=="null"){
      	return;
      }
      if(bo){
        __alert("增加岗位成功！");
        ATree.reload(pid);//刷新当前父节点
      }else{
      	__alert("增加岗位失败！");
      }
        doCloseWin();
    }


    /**
     * 执行修改的相关操作
     */
    function __invokeEditAction($type, $fix) {
       if ($type=="org.edit") { //编辑单位信息
        var p = __get("PanelOrgEdit");
        var nodeUuid = _activeNode.key;

        //读取这个节点的详细资料
        _Xuice.url("xml/org_info_xml.jsp?uuid=" + nodeUuid, finishA, false);
        function finishA($doc) {
          var obj = _Xuice.xmlObj($doc.documentElement);
          with(p.all) {
            parentid.value = obj.parentid;
            depname.value = obj.depname;
            deplink.value = obj.deplink;
            depcontent.value = obj.depcontent;
            deporder.value = obj.deporder;
            depnumber.value = obj.depnumber;
            ParentName.innerHTML = obj.parentname;
            depid.value = obj.depid;
            /*fsPhone.value = obj.fsphone;
            fsFax.value = obj.fsfax;
            fsProvince.value = obj.fsprovince;
            fsCity.value = obj.fscity;
            fsAddress.value = obj.fsaddress;
            fsPostNumber.value = obj.fspostnumber;
            fsShortName.value = obj.fsshortname;*/
          }
          __xopen(p, "width=260;height=120;", $fix);
        }
      }else if ($type=="dept.edit") { //部门信息
        var p = __get("PanelDeptEdit");
        var nodeUuid = _activeNode.key;

        //读取这个节点的详细资料
        _Xuice.url("xml/dept_info_xml.jsp?uuid=" + nodeUuid, finishB, false);
        function finishB($doc) {
          var obj = _Xuice.xmlObj($doc.documentElement);
          with(p.all) {
          	parentid.value = obj.parentid;
            depid.value = obj.depid;
            deplink.value = obj.deplink;
            depname.value = obj.depname;
            depcontent.value = obj.depcontent;
            deporder.value = obj.deporder;
            ParentName.innerHTML = obj.parentname;
          }
          __xopen(p, "width=250;height=120", $fix);
        }
      }else if ($type=="pos.edit") {  //岗位信息
        var p = __get("PanelPosEdit");
        var nodeUuid = _activeNode.key;

        //读取这个节点的详细资料
        _Xuice.url("xml/pos_info_xml.jsp?uuid=" + nodeUuid, finishC, false);
        function finishC($doc) {
          var obj = _Xuice.xmlObj($doc.documentElement);
          with(p.all) {
          	parentid.value = obj.parentid;
            depid.value = obj.depid;
            deplink.value = obj.deplink;
            depname.value = obj.depname;
            depcontent.value = obj.depcontent;
            deporder.value = obj.deporder;
            ParentNameOrg.innerHTML = obj.parentnameOrg;
            ParentName.innerHTML = obj.parentname;
          }
          __xopen(p, "width=250;height=120", $fix);
        }
      }else if ($type=="role.edit") {
        var k = parseRoleKey(_activeNode.key);
        __open("role_edit.jsp?roleUuid=" + k, "width=500;height=150;center=true;fresh=true");
      }
    }

    /**
     * 修改组织单位的信息
     */
    function doOrgEditSave() {
      var table = "Hzsysorganization";
      var names = "depid,parentid,depname,deplink,depnumber,deporder,depcontent";
      var d = __getReal(event.srcElement, "id", "XuiceForm");
      var pid = d.all.parentid.value;
      var bo = updateXuiceForm( d ,names,table);
      if(bo=="null"){
      	return;
      }
      if(bo){
        __alert("修改单位成功！");
        ATree.reload(pid);//刷新当前父节点
      }else{
      	__alert("修改单位失败！");
      }
        doCloseWin();
    }

    /**
     * 修改部门信息
     */
    function doDeptEditSave() {
      var table = "Hzsysorganization";
      var names = "depid,parentid,depname,deplink,deporder,depcontent";
      var d = __getReal(event.srcElement, "id", "XuiceForm");
      var pid = d.all.parentid.value;
      var bo = updateXuiceForm( d ,names,table);
      if(bo=="null"){
      	return;
      }
      if(bo){
        __alert("修改部门成功！");
        ATree.reload(pid);//刷新当前父节点
      }else{
      	__alert("修改部门失败！");
      }
        doCloseWin();
    }

    /**
     * 修改岗位信息
     */
    function doPosEditSave() {
      var table = "Hzsysorganization";
      var names = "depid,parentid,depname,deporder,depcontent";
      var d = __getReal(event.srcElement, "id", "XuiceForm");
      var pid = d.all.parentid.value;
      var bo = updateXuiceForm( d ,names,table);
      if(bo=="null"){
      	return;
      }
      if(bo){
        __alert("修改岗位成功！");
        ATree.reload(pid);//刷新当前父节点
      }else{
      	__alert("修改岗位失败！");
      }
        doCloseWin();
    }

  /**
   * 增加职员
   */
  function doAddEmployee() {
    var url = "emp_add.jsp?orgId=" + __trim(_currentOrgId) + "&deptId=" + __trim(_currentDeptId) + "&posId=" + __trim(_currentPosId);
    __open(url, "width=600;height=380;center=true;fresh=true");
  }

  /**
   * 增加职员成功
   */
  function onAddEmployeeFinish() {
    reloadEmpLister();
  }

  //修改职员资料成功
  function onEditEmployeeFinish() {
    reloadEmpLister();
  }

  function renderFinish() {
    setupTreeHeight();
  }
  
  //设置树的高度
  function setupTreeHeight() {
  	try{
    	var d = ATree.getRootDom();
	    var pos = __getPos(d);
	    var p = document.body.clientHeight - pos.y;
	    d.style.height = p-6-33;
    }catch(e){}
    //window.attachEvent("onresize", setupTreeHeight);
  }
  window.attachEvent("onresize", setupTreeHeight);
//层数据的保存
function saveXuiceForm(elements,names,table) {
	var values = cellvalue(elements,names);
    if(values){
    	var boon=false;
    	DWREngine.setAsync(false);
		ajax.saveForm(values,names,table,function(betm){
			boon=betm;
		});
		return boon;
	}else{
		return "null";
	}
}
//层数据的修改
function updateXuiceForm(elements,names,table) {
  	var values = cellvalue(elements,names);
    if(values){
    	var boon=false;
    	DWREngine.setAsync(false);
		ajax.updateForm(values,names,table,function(betm){
			boon=betm;
		});
		return boon;
	}else{
		return "null";
	}
}
//删除操作
function deleteTree(key,type) {
	var boon="";
	DWREngine.setAsync(false);
	ajax.delTree(key,type,function(betm){
		boon=betm;
	});
	return boon;
}
//刷新人员列表信息
function doSelper(odpid) {
	if(odpid.search=="false"){
		if(odpid.nopos==true){
			jsonPage_sql = "EM02";
			jsonPage_where = "";
		}else{
			jsonPage_sql = "EM01";
			if(odpid.orgId !=""){
				jsonPage_where = " and p.orgid='"+odpid.orgId+"'";
			}
			if(odpid.deptId!=""){
				jsonPage_where = " and p.deptid='"+odpid.deptId+"'";
			}
			if(odpid.posId !=""){
				jsonPage_where = " and p.posid ='"+odpid.posId+"'";
			}
		}
	}else{
		jsonPage_where = "";
		jsonPage_sql = "EM01";
		if(odpid.keyName != ""){
			jsonPage_where += " and p.personname like '%"+odpid.keyName+"%'";
		}
		if(odpid.keyPost != ""){
			jsonPage_where += " and dp.depname like '%"+odpid.keyPost+"%'";
		}
	}
	if(jsonPage_where.indexOf(" order")==-1){
		//jsonPage_where += " and p.userid='"+__get("userid").value+"' order by perdrder";
		jsonPage_where += " order by perdrder";
	}
	jsonPage_state = "hql";
	jsonPage_action = "first";
	DWREngine.setAsync(false);
	setTimeout('doPaging()',50);
}
function on_Jsonpaging_Back(){
	EMPLister_loaded();
}